//
//  VSFindOtherPushViewController.m
//  UI项目2.0
//
//  Created by dlios on 15/7/16.
//  Copyright (c) 2015年 星际科技. All rights reserved.
//

#import "VSFindOtherPushViewController.h"
#import "UIImageView+WebCache.h"
#import "MBProgressHUD+MJ.h"
#import "VSFrameGrand.h"
@interface VSFindOtherPushViewController ()  <UIAlertViewDelegate>

@property (nonatomic, retain) UIImageView *imageView;
@property (nonatomic, retain) UIView *Topview;
@property (nonatomic, retain) UIView *TopGroundView;
@property(nonatomic, assign, getter=upOrdown)BOOL up;

@end

@implementation VSFindOtherPushViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor blackColor];
    self.parentViewController.tabBarController.tabBar.hidden = YES;
    self.navigationController.navigationBarHidden = YES;
    
    
    
    [self viewShow];
    
    
}

- (void)viewShow
{
    if (self.FromGiftToOtherModel != nil) {
        CGFloat photoHeight = SCREEN_WIDTH / [[_FromGiftToOtherModel.photo objectForKey:@"width"] floatValue] * [[_FromGiftToOtherModel.photo objectForKey:@"height"] floatValue];
        CGFloat photoWidth = SCREEN_HEIGHT / [[_FromGiftToOtherModel.photo objectForKey:@"height"] floatValue] * [[_FromGiftToOtherModel.photo objectForKey:@"width"] floatValue];
        if (photoHeight > SCREEN_HEIGHT) {
            photoHeight = SCREEN_HEIGHT;
            
        } else {
            photoWidth = SCREEN_WIDTH;
        }
        
        self.imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, photoWidth, photoHeight)];
        self.imageView.center = CGPointMake(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2);
        [self.imageView setImageWithURL:[NSURL URLWithString:[_FromGiftToOtherModel.photo objectForKey:@"path"]]];
    }  else {
        self.imageView = [[UIImageView alloc]initWithFrame:self.view.bounds];
        [self.imageView setImageWithURL:[NSURL URLWithString:self.image]];
    }
    
    
    [self.view addSubview:self.imageView];
    self.imageView.userInteractionEnabled = YES;
    
    self.TopGroundView = [[[UIView alloc]initWithFrame:CGRectMake(0, -44, SCREEN_WIDTH, 44)] autorelease];
    self.TopGroundView.backgroundColor = [UIColor grayColor];
    self.TopGroundView.alpha = 0.7;
    [self.view addSubview:self.TopGroundView];
    
    
    self.Topview = [[[UIView alloc]initWithFrame:CGRectMake(0, -44, SCREEN_WIDTH, 44)] autorelease];
    self.Topview.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.Topview];
    [self buttonShow];
    
    
    
    
    // 创建手势
    UILongPressGestureRecognizer *LongPress = [[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressAction:)] autorelease];
    [self.imageView addGestureRecognizer:LongPress];
    
    
    
    UITapGestureRecognizer *tap = [[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapOnlyAction)] autorelease];
    tap.numberOfTapsRequired = 1;
    [self.imageView addGestureRecognizer:tap];
    
    UIPinchGestureRecognizer *pinch = [[[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchAction:)] autorelease];
    [self.imageView addGestureRecognizer:pinch];
}



- (void)buttonShow
{
    
    
    UIButton *TopLeftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    TopLeftButton.backgroundColor = [UIColor clearColor];
    TopLeftButton.frame = CGRectMake(8, 10, 55, 25);
    [TopLeftButton setImage:[UIImage imageNamed:@"iconfont005"] forState:UIControlStateNormal];
    [TopLeftButton addTarget:self action:@selector(showBackGround) forControlEvents:UIControlEventTouchUpInside];
    [self.Topview addSubview:TopLeftButton];
    
    UIButton *TopRightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    TopRightButton.backgroundColor = [UIColor clearColor];
    TopRightButton.frame = CGRectMake(SCREEN_WIDTH - 8 - 55, 10, 55, 25);
    [TopRightButton addTarget:self action:@selector(longPressAction:) forControlEvents:UIControlEventTouchUpInside];
    [TopRightButton setTitle:@"保存" forState:UIControlStateNormal];
    [self.Topview addSubview:TopRightButton];
}





- (BOOL)prefersStatusBarHidden
{
    return YES;
}

- (void)tapOnlyAction
{
    self.up = !self.up;
    
    if ( self.TopGroundView.frame.origin.y == - 44 && self.up == YES) {
        [UIView animateWithDuration:0.7 animations:^{
            self.TopGroundView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 44);
            self.Topview.frame = CGRectMake(0, 0, SCREEN_WIDTH, 44);
        }];
        
    }
    if ( self.TopGroundView.frame.origin.y == 0 && self.up == NO) {
        [UIView animateWithDuration:0.7 animations:^{
            self.TopGroundView.frame = CGRectMake(0, - 44, SCREEN_WIDTH, 44);
            self.Topview.frame = CGRectMake(0, -44, SCREEN_WIDTH, 44);
        }];
        
    }
    
}


- (void)longPressAction:(UILongPressGestureRecognizer *)longPress
{
    if (longPress.state == UIGestureRecognizerStateBegan) {
        self.imageView.userInteractionEnabled = NO;
        UIAlertView *alv = [[UIAlertView alloc] initWithTitle:@"保存到本地相册" message:nil delegate:self cancelButtonTitle:@"否" otherButtonTitles:@"是", nil];
        alv.delegate = self;
        [alv show];
    }
}

- (void)pinchAction:(UIPinchGestureRecognizer *)pinch
{
    UIImageView *img = (UIImageView *)pinch.view;
    [img setTransform:CGAffineTransformScale(img.transform, pinch.scale, pinch.scale)];
    pinch.scale = 1;
}



- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1) {
        self.imageView.userInteractionEnabled = YES;
        [self saveImage:self.imageView.image];
    } else if (buttonIndex == 0) {
        self.imageView.userInteractionEnabled = YES;
    }
}

- (void)saveImage:(UIImage *)image
{
    UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
    
}




// 指定回调方法
- (void)image: (UIImage *) image didFinishSavingWithError: (NSError *) error contextInfo: (void *) contextInfo
{
    if(error != NULL){
        [MBProgressHUD showError:@"失败"];
    }else{
        [MBProgressHUD showSuccess:@"已保存"];
    }
    
}

- (void)showBackGround
{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
    self.Topview.hidden = YES;
    self.parentViewController.tabBarController.tabBar.hidden = NO;
    self.navigationController.navigationBarHidden = NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
